import sys
import chilkat

json = chilkat.CkJsonObject()

#  Load the JSON from a file.
success = json.LoadFile("modifySample.json")
if (success != True):
    print(json.lastErrorText())
    sys.exit()

#  This example will not check for errors (i.e. null / false / 0 return values)...

#  Get the "list" array:
# listA is a CkJsonArray
listA = json.ArrayOf("list")

#  Modify values in the list.

#  Change banana to plantain
success = listA.SetStringAt(0,"plantain")

#  Change 12 to 24
success = listA.SetIntAt(1,24)

#  Change true to false
success = listA.SetBoolAt(2,False)

#  Is the 3rd item null?
bNull = listA.IsNullAt(3)

#  Change "orange" to 32.
success = listA.SetIntAt(4,32)

#  Change 12.5 to 31.2
success = listA.SetNumberAt(5,"31.2")

#  Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
#  Do this by deleting, then inserting a new object at the same location.
success = listA.DeleteAt(6)
success = listA.AddObjectAt(6)
# tickerObj is a CkJsonObject
tickerObj = listA.ObjectAt(6)
success = tickerObj.AddStringAt(-1,"ticker","GOOG")

#  Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
success = listA.DeleteAt(7)
success = listA.AddArrayAt(7)
# aa is a CkJsonArray
aa = listA.ArrayAt(7)
success = aa.AddStringAt(-1,"apple")
success = aa.AddIntAt(-1,22)
success = aa.AddBoolAt(-1,True)
success = aa.AddNullAt(-1)
success = aa.AddNumberAt(-1,"1080.25")

#  Get the "fruit" array
# aFruit is a CkJsonArray
aFruit = json.ArrayAt(0)

#  Get the 1st element:
# appleObj is a CkJsonObject
appleObj = aFruit.ObjectAt(0)

#  Modify values by member name:
success = appleObj.SetStringOf("fruit","fuji_apple")
success = appleObj.SetIntOf("count",46)
success = appleObj.SetBoolOf("fresh",False)
success = appleObj.SetStringOf("extraInfo","developed by growers at the Tohoku Research Station in Fujisaki")

#  Modify values by index:
# pearObj is a CkJsonObject
pearObj = aFruit.ObjectAt(1)
success = pearObj.SetStringAt(0,"bartlett_pear")
success = pearObj.SetIntAt(1,12)
success = pearObj.SetBoolAt(2,False)
success = pearObj.SetStringAt(3,"harvested in late August to early September")

json.put_EmitCompact(False)
print(json.emit())
